package com.example.nophase.activity.wageGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.nophase.R;
import com.example.nophase.activity.finance.CompleteInstructionsActivity;
import com.example.nophase.activity.finance.WorkDetailFormActivity;
import com.example.nophase.adapter.CompletelAdapter;
import com.example.nophase.api.RequstCommonApi;
import com.example.nophase.base.BaseActivity;
import com.example.nophase.common.BaseCallBack;
import com.example.nophase.common.BaseResp;
import com.example.nophase.common.LoginManager;
import com.example.nophase.entity.InstructionsDTO;
import com.example.nophase.entity.RecordsDTO;
import com.example.nophase.entity.SysUserVO;
import com.example.nophase.utils.HttpUtils;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class BatchOfPayCompleteActivity extends BaseActivity {

    @BindView(R.id.applyfor_toolbar)
    Toolbar applyforToolbar;
    @BindView(R.id.applyfor_lv)
    ListView applyforLv;
    @BindView(R.id.applyfor_list_refreshLayout)
    SmartRefreshLayout applyforListRefreshLayout;
    List<InstructionsDTO> resultList = new ArrayList<>();
    public CompletelAdapter adapter;

    public int page = 1;
    @BindView(R.id.iv_background)
    ImageView ivBackground;
    private int dataSize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batch_of_pay_complete);
        ButterKnife.bind(this);
        setToolbar();
        adapter = new CompletelAdapter(resultList, this);
        applyforLv.setAdapter(adapter);
        applyforListRefreshLayout.setRefreshHeader(new WaterDropHeader(this));
        applyforListRefreshLayout.setRefreshFooter(new BallPulseFooter(this));
        setRefreshListener();
        initData();
        applyforLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BatchOfPayCompleteActivity.this, WorkDetailFormActivity.class);
                intent.putExtra("forkey", String.valueOf(resultList.get(position).getId()));
                intent.putExtra("type", "工资组");
                startActivity(intent);
            }
        });
    }

    private void initData() {

        Call<BaseResp<RecordsDTO<InstructionsDTO>>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).lodaAdultComplete("ANDROID", 50, 1);
        call.enqueue(new BaseCallBack<BaseResp<RecordsDTO<InstructionsDTO>>>() {
            @Override
            public void onSuccess(Response<BaseResp<RecordsDTO<InstructionsDTO>>> response) throws Exception {
                List<InstructionsDTO> list = response.body().getData().getRecords();

                if(list.size()> 0){
                    ivBackground.setVisibility(View.GONE);
                }
                dataSize = list.size();
                for (InstructionsDTO item : list) {
                    resultList.add(item);
                    adapter.notifyDataSetChanged();
                }

                applyforListRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }

            @Override
            public void onFailure(String message) {

            }
        });

    }


    private void setRefreshListener() {
        applyforListRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                resultList.clear();
                page = 1;

                Call<BaseResp<RecordsDTO<InstructionsDTO>>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).lodaAdultComplete("ANDROID", 50, 1);
                call.enqueue(new BaseCallBack<BaseResp<RecordsDTO<InstructionsDTO>>>() {
                    @Override
                    public void onSuccess(Response<BaseResp<RecordsDTO<InstructionsDTO>>> response) throws Exception {
                        refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                        List<InstructionsDTO> list = response.body().getData().getRecords();
                        dataSize = list.size();
                        for (InstructionsDTO item : list) {
                            resultList.add(item);
                            adapter.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void onFailure(String message) {

                    }
                });
            }
        });
        applyforListRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000/*,false*/);
                if (dataSize < 10) {
                    Toast.makeText(BatchOfPayCompleteActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
                } else {
                    page++;

                    Call<BaseResp<RecordsDTO<InstructionsDTO>>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).lodaAdultComplete("ANDROID", 50, page);
                    call.enqueue(new BaseCallBack<BaseResp<RecordsDTO<InstructionsDTO>>>() {
                        @Override
                        public void onSuccess(Response<BaseResp<RecordsDTO<InstructionsDTO>>> response) throws Exception {

                            List<InstructionsDTO> list = response.body().getData().getRecords();
                            dataSize = list.size();
                            for (InstructionsDTO item : list) {
                                resultList.add(item);
                                adapter.notifyDataSetChanged();
                            }

                        }

                        @Override
                        public void onFailure(String message) {

                        }
                    });

                }

            }
        });
    }


    private void setToolbar() {
        applyforToolbar.setNavigationIcon(R.drawable.ic_left_black_24dp);
        applyforToolbar.setTitle("");
        setSupportActionBar(applyforToolbar);
        getSupportActionBar().setHomeButtonEnabled(true);


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                this.finish();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

}